{% extends "../base.html" %}

{% block title %} {% end %}
{% block css %} {% end %}
{% block body %}
    <div class="layui-body">
    {% include _form.html %}
    </div>
 {% end %}

{% block javascript %}
<script src="{{ static_url('js/jsencrypt.js') }}" charset="utf-8"></script>
<script type="text/javascript">
layui.use(['form'], function() {
    var $ = layui.jquery, form = layui.form

    // 表单验证
    form.verify({
        username : [/[A-Za-z0-9\u4e00-\u9fa5]{2,40}$/, '用户名必须2到40位字母、数字、汉字组合'],
        // password : [/(.+){8,40}$/, '8到40个任意字符'],
        number : [/^[0-9]*$/, '必须输入数字啊']
    })
    form.on('submit(formSubmit)', function(obj) {
        layer.msg('数据提交中...',{time:50000})

        if (rsa_encrypt=='1' && obj.field.password) {
            // Encrypt with the public key...
            var encrypt = new JSEncrypt()
            encrypt.setPublicKey(public_key)
            var ciphertext = encrypt.encrypt(obj.field.password)
            obj.field.password = ciphertext
        }

        $.ajax({
            type: "POST",
            url: obj.form.action,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('更新成功', {icon: 1, time: 2000}, function(){
                        parent.edit_success(res.data)
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='username']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            },
            error: function(xhr){
                // console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false;
    })
})
</script>
 {% end %}
